﻿/// <reference path="../../extjs/ext-all-debug.js" />
Ext.define('yzmh.controller.Diqus', {
    extend: 'Ext.app.Controller',

    stores: ['Diqus'],

    models: ['Diqu'],

    views: ['diqu.List', 'diqu.Edit'],

    refs: [{
        ref: 'tbar',
        selector: 'diqulist toolbar'
    },{
        ref: 'grid',
        selector: 'diqulist'
    }, {
        ref: 'progressbar',
        selector: 'diqulist progressbar'
    }],

    init: function () {
        this.control({
            'diqulist': {
                render: this.loadDiqus,
                itemdblclick: this.editDiqu
            },
            'diqulist button[action=add]': {
                click: this.addDiqu
            },
            'diqulist button[action=delete]': {
                click: this.deleteDiqu
            },
            'diqulist button[action=htmlall]': {
                click: this.createHtmlAll
            },
            'diqulist actioncolumn[action=view]': {
                click: this.view
            },
            'diquedit button[action=save]': {
                click: this.saveDiqu
            },
            'diqulist dataview': {
                drop: this.sortDiqu
            }
        });
    },

    view: function (o, item, rowIndex, colIndex, e, row) {
        window.open("/list/" + row.get('filename'), '_blank');
    },

    sortDiqu: function (node, data, dropRec, dropPosition) {
        var oldid = [], newid = [], store = this.getDiqusStore();
        Ext.Msg.confirm("提示", "你需要立即执行排序吗？", function (result) {
            if (result == "yes") {
                store.each(function (record) {
                    oldid.push(record.get('id'));
                    newid.push(record.get('id'));
                });
                oldid.sort(function (a, b) { return a - b; });
                var data = [];
                store.each(function (record, i) {
                    if (oldid[i] != newid[i]) {
                        data.push(Ext.apply(record.raw, {
                            id: oldid[i]
                        }));
                    }
                });
                Ext.Ajax.request({
                    url: 'data/diqu.aspx?a=sort',
                    params: { data: Ext.encode(data) },
                    method: 'POST',
                    success: function (response, opts) {
                        var result = Ext.decode(response.responseText);
                        if (result.success) {
                            store.reload();
                        } else {
                            Ext.Msg.alert("错误", result.errorMessage);
                        }
                    }
                });
            }
        });
    },

    addDiqu: function (grid, record) {
        var edit = Ext.create('yzmh.view.diqu.Edit').show();
    },

    editDiqu: function (grid, record) {
        var edit = Ext.create('yzmh.view.diqu.Edit').show();
        edit.down('form').loadRecord(record);
    },

    deleteDiqu: function () {
        var rows = this.getGrid().getSelectionModel().getSelection();
        var store = this.getDiqusStore();
        var ids = [];
        Ext.each(rows, function (r) {
            ids.push(r.get("id"));
        });
        Ext.Msg.confirm("提示", "确定要删除选中的" + rows.length + "个友情链接吗？", function (result) {
            if (result == "yes") {
                Ext.Ajax.request({
                    url: 'data/diqu.aspx?a=delete',
                    params: { ids: ids.join(',') },
                    method: 'POST',
                    success: function (response, opts) {
                        var result = Ext.decode(response.responseText);
                        if (result.success) {
                            store.remove(rows);
                        } else {
                            Ext.Msg.alert("错误", result.message);
                        }
                    }
                });
            }
        });
    },

    saveDiqu: function (button, e) {
        var win = button.up('window'),
            form = win.down('form'),
            record = form.getRecord(),
            values = form.getValues(),
            store = this.getDiqusStore();
        if (form.getForm().isValid()) {
            form.submit({
                url: 'data/diqu.aspx?a=save',
                success: function (form, action) {
                    win.close();
                    store.reload();
                },
                failure: function (form, action) {
                    Ext.Msg.alert('Failed', action.result ? action.result.errorMessage : 'No response');
                }
            });
        }
    },

    loadDiqus: function (grid) {
        this.getDiqusStore().load();
    },

    createHtmlAll: function () {
        var store = this.getDiqusStore();
        var pbar = this.getProgressbar();
        var v = 0;
        function createHtml(row) {
            Ext.Ajax.request({
                url: 'data/makehtml.aspx?a=diqu',
                params: { classid: row.get('id') },
                method: 'POST',
                success: function (response, opts) {
                    v++;
                    pbar.updateProgress(v / store.getCount(), '生成HTML页面 ' + v + ' / ' + store.getCount() + '...');
                    if (store.getAt(v) != null) {
                        createHtml(store.getAt(v));
                    } else {
                        pbar.updateText('全部类别生成HTML完成')
                    }
                }
            });
        }
        Ext.Msg.confirm("提示", "确定要生成全部类别吗？", function (result) {
            if (result == "yes") {
                pbar.reset();
                pbar.show();
                rows = result.allid;
                createHtml(store.getAt(v))
            }
        });
    }
});
